package com.arcaneshift.model.validators.user;

import com.arcaneshift.model.request.user.AddUserRequest;
import com.arcaneshift.security.errors.CredentialsError;
import com.arcaneshift.security.errors.UserError;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.util.UserType;
import com.common.security.abstracts.AbstractError;
import com.common.security.errors.ParserError;

public class AddUserValidator extends AbstractValidator {

	@Override
	public boolean validate(AbstractRequest abstractRequest) {
		AddUserRequest request = null;
		
		if (abstractRequest instanceof AddUserRequest) {
			request = (AddUserRequest) abstractRequest;
		} else {
			error = new UserError(AbstractError.INVALID_REQUEST);
			return false;
		}
		
		String username = request.getUsername();
		String password = request.getPassword();
		UserType userType = request.getUserType();
		
		if (username == null) {
			error = new CredentialsError(CredentialsError.NO_USERNAME_PROVIDED);
			return false;
		}
		
		if (password == null) {
			error = new CredentialsError(CredentialsError.NO_PASSWORD_PROVIDED);
			return false;
		}
		
		if (userType == null) {
			error = new ParserError(ParserError.USER_TYPE_NOT_PROVIDED);
			return false;
		}
		
		if (username.length() < LoginValidator.USERNAME_MIN_LENGTH || 
				username.length() > LoginValidator.USERNAME_MAX_LENGTH) {
			error = new CredentialsError(CredentialsError.USERNAME_BAD_FORMAT);
			return false;
		}
		
		if (password.length() < LoginValidator.PASSWORD_MIN_LENGTH || 
				password.length() > LoginValidator.PASSWORD_MAX_LENGTH) {
			error = new CredentialsError(CredentialsError.PASSWORD_BAD_FORMAT);
			return false;
		}
		
		return true;
	}

}
